package com.hzit.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.R;
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.util.Calendar;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Configuration
public class GatewaySentinelConfig {

	@PostConstruct
	public void init(){
		GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
			@Override
			public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t) {
				R<String> fail = R.fail("出现了网关限流异常！ ");

				Mono<String> just = Mono.just(JSON.toJSONString(fail));
				Mono<ServerResponse> body = ServerResponse.ok().body(just, String.class);
				return body;
			}
		});
	}

}
